Shader "Lyf/Decal/LScreenSpaceDecalNoice"
{
    Properties
    {
        _TintColor("TintColor", Color) = (1,1,1,1)
        _MainTex ("Texture", 2D) = "white" {}

        [Space]
        [Toggle(USE_NOISE_DISTORTION)] _UseNoiseDistortion("Use Noise Distortion", Int) = 0
        _NoiseTex("Noise Texture (RG)", 2D) = "gray" {}
        _DistortSpeed("Distort Speed", Float) = 1
        _DistortScale("Distort Scale", Float) = .1
        [Space][Space][Space]

        _CutoutTex("Cuout Tex", 2D) = "white" {}

        _Cutout("Cutout", Range(0.0, 1.0)) = 1
        _CutoutAlphaMul("Alpha multiplier", Float) = 1

        _SrcMode("SrcMode", int) = 5
        _DstMode("DstMode", int) = 10
        _ZTest1("_ZTest1", int) = 5
    }
    SubShader
    {
        Tags { "Queue" = "Geometry+1"  "IgnoreProjector" = "True" "RenderType" = "Transparent" }
        Blend [_SrcMode][_DstMode]
        Cull Front
        ZTest [_ZTest1]
        ZWrite Off
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma shader_feature USE_NOISE_DISTORTION

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float4 color:COLOR;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float4 color : COLOR;
                float3 ray : TEXCOORD0; 
                float4 screenUV : TEXCOORD1;
            };

            sampler2D _CameraDepthTexture;
            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _NoiseTex;
            float4 _NoiseTex_ST;
            sampler2D _CutoutTex;
            float4 _CutoutTex_ST;

            half _Cutout;
            half _CutoutAlphaMul;
            half4 _TintColor;

            half _DistortSpeed;
            half _DistortScale;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                //o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.color = v.color;
                o.ray = UnityObjectToViewPos(v.vertex) * float3(-1,-1,1);
                o.screenUV = ComputeScreenPos(o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                i.ray *= (_ProjectionParams.z / i.ray.z);//Far clip dist/viewspace distance
                float depth = Linear01Depth(tex2Dproj(_CameraDepthTexture,i.screenUV));
                float3 wpos = mul(unity_CameraToWorld, float4(i.ray*depth, 1)).xyz;
                float3 opos = mul(unity_WorldToObject, float4(wpos,1)).xyz;
                float3 stepVal = saturate((0.5 - abs(opos.xyz)) * 10000);
                float projClipFade = stepVal.x * stepVal.y * stepVal.z;

                float2 uvCutout = (opos.xz + 0.5) * _CutoutTex_ST.xy + _CutoutTex_ST.zw;
                // sample the texture
                float2 uv = opos.xz + 0.5;
                float2 uvMain = uv * _MainTex_ST.xy + _MainTex_ST.zw;
                

                #if defined (USE_NOISE_DISTORTION)
                float2 uvNoise = uv * _NoiseTex_ST.xy + _NoiseTex_ST.zw;
                half2 distortMask = tex2D(_NoiseTex, uvNoise) * 2 - 1;
                half4 col = tex2D(_MainTex, uvMain + distortMask * _DistortScale + _DistortSpeed *_Time.xx);
                half4 col2 = tex2D(_MainTex, uvMain - distortMask * _DistortScale - _DistortSpeed * _Time.xx * 1.4);
                col *= col2;
                #else
                fixed4 col = tex2D(_MainTex, uvMain);
                #endif

                half4 tintColor = _TintColor;
                tintColor.rgb = tintColor.rgb * tintColor.rgb * 2;
                col *= tintColor;

                half mask = tex2D(_CutoutTex, uvCutout).a;
                half cutout = 1 - _Cutout * i.color.a;
                mask = saturate((mask - cutout) * _CutoutAlphaMul) * col.a;
                col.a = mask;

                col.a = saturate(col.a * projClipFade);
                return col;
            }
            ENDCG
        }

        Pass
            {
                Tags{ "Queue" = "Transparent" "LightMode" = "ShadowCaster" }
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma multi_compile_shadowcaster
                #pragma fragmentoption ARB_precision_hint_fastest
                #include "UnityCG.cginc"

                struct appdata
                {
                    float4 vertex : POSITION;
                    half3 normal : NORMAL;
                };


                struct v2f
                {
                    V2F_SHADOW_CASTER;
                };

                v2f vert(appdata v)
                {
                    v2f o;
                    TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
                    return o;
                }

                float4 frag(v2f i) : COLOR
                {
                    SHADOW_CASTER_FRAGMENT(i)
                }

                ENDCG
            }
    }
    //CustomEditor "LStudyShaderGUI"
}
